# 导入必要的模块
import cv2
from image_utils import logging
from skimage import io  # skimage.io模块用于读写图像文件

def load_image(image_path, as_gray=False):
    try:
        logging.info(f"开始载入图像: {image_path}")

        # 载入图像文件的数据,如果as_gray为True,则载入灰度图像
        image = io.imread(image_path, as_gray=as_gray)
        if as_gray and image.dtype == 'float64':
            image = (image * 255).astype('uint8')

        logging.info(f"成功载入图像: {image_path}")
        return image
    except Exception as e:
        logging.error(f"载入图像时发生错误: {e}")
        return None
def save_image(image, save_path, format='png'):
    try:
        logging.info(f"开始保存图像: {save_path}")

        # 将图像 image 保存到文件 save_path
        io.imsave(save_path, image, format)

        logging.info(f"图像成功保存在: {save_path}")
    except Exception as e:
        logging.error(f"保存图像时发生错误: {e}")
